import type { PostLoginRes } from '@/types/common'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    // 持久化存储token
    const token = ref('')
    const setToken = (value: string) => {
      token.value = value
    }
    const getToken = () => {
      return token.value
    }
    const removeToken = () => {
      token.value = ''
    }

    // 用户信息
    const userInfo = ref<PostLoginRes>()
    const setUserInfo = (value: PostLoginRes) => {
      userInfo.value = value
    }
    const clearUserInfo = () => {
      userInfo.value = {} as PostLoginRes
    }

    return { token, setToken, getToken, removeToken, userInfo, setUserInfo, clearUserInfo }
  },
  {
    persist: true,
  }
)
